package com.pearadmin.pro.common.web.domain;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * Ajax响应实体
 *
 * @author wangxiaozhe
 * CreateTime: 2019/10/23
 */
@Data
@Accessors(chain = true)
@SuppressWarnings("unused")
public class Result<T> {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 提示消息
     */
    private String msg;

    /**
     * 返回结果
     */
    private boolean success;

    /**
     * 携带数据
     */
    private Object data;

    /**
     * 时间戳
     */
    private long timeStamp = System.currentTimeMillis();

    /**
     * 权鉴
     */
    private String tokenKey;

    private String token;

    /**
     *成功操作
     */
    public static<T> Result<T> success(){
        return success("");
    }

    /**
     *成功操作,携带数据
     */
    public static<T> Result<T> success(T data){
        return success(ResultCode.SUCCESS.message(),data);
    }

    /**
     *成功操作,携带消息
     */
    public static<T> Result<T> success(String message){
        return success(message,null);
    }

    /**
     *成功操作,携带消息和携带数据
     */
    public static<T> Result<T> success(String message,T data){
        return success(ResultCode.SUCCESS.code(),message,data);
    }

    /**
     *成功操作,携带自定义状态码和消息
     */
    public static<T> Result<T> success(ResultCode resultCode){
        return success(resultCode.code(),resultCode.message());
    }

    /**
     *成功操作,携带自定义状态码和消息
     */
    @SuppressWarnings({"unchecked","rawtypes"})
    public static<T> Result<T> success(ResultCode resultCode,String tokenKey,String token){
        Result result = success(resultCode.code(),resultCode.message());
        result.setTokenKey(tokenKey);
        result.setToken(token);
        return result;
    }

    public static<T> Result<T> success(int code,String message){
        return success(code,message,null);
    }

    /**
     *成功操作,携带自定义状态码,消息和数据
     */
    public static<T> Result<T> success(int code,String message,T data){
        return result(code,message,data,true);
    }

    /**
     *失败操作,默认数据
     */
    public static<T> Result<T> failure(){
        return failure(ResultCode.FAILURE.message());
    }

    /**
     * 失败操作,携带自定义消息
     */
    public static<T> Result<T> failure(String message){
        return failure(message,null);
    }

    /**
     *失败操作,携带自定义消息,状态码
     */
    public static<T> Result<T> failure(ResultCode resultCode){
        return failure(resultCode.code(),resultCode.message());
    }

    /**
     *失败操作,携带自定义消息和数据
     */
    public static<T> Result<T> failure(String message,T data){
        return failure(ResultCode.FAILURE.code(),message,data);
    }

    /**
     *失败操作,携带自定义状态码和自定义消息
     */
    public static<T> Result<T> failure(int code ,String message){
        return failure(code,message,null);
    }

    /**
     *失败操作,携带自定义状态码,消息和数据
     */
    public static<T> Result<T> failure(int code,String message,T data){
        return result(code,message,data,false);
    }

    /**
     * Boolean返回操作,携带默认返回值
     */
    public static<T> Result<T> auto(boolean b){
        return auto(b,ResultCode.SUCCESS.message(),ResultCode.FAILURE.message());
    }

    /**
     * Boolean返回操作,携带自定义消息
     */
    public static<T> Result<T> auto(boolean b,String success,String failure){
        if(b){
            return success(success);
        }else{
            return failure(failure);
        }
    }

    /**
     * Result构建方法
     */
    public static<T> Result<T> result(int code,String message,T data,boolean success){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(message);
        result.setSuccess(success);
        result.setTimeStamp(System.currentTimeMillis());
        result.setData(data);
        return result;
    }

}
